package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.component.TokenComponent;
import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.model.po.Article;
import com.lebang.medicalInquiry.model.po.ArticleClassify;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/api/article")
@Api(tags = "文章相关")
@RestController
@Validated
public class ArticleApi {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TokenComponent tokenComponent;


    @GetMapping("/classify")
    @ApiOperation("获取所有文件分类标签")
    public JsonData<List<ArticleClassify>> getArticleClassify(HttpServletRequest request, HttpServletResponse response) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), articleService.getArticleClassfiy());

    }

    @GetMapping("/")
    @ApiOperation("根据搜索条件查询文章")
    public JsonData<List<Article>> getArticle(HttpServletRequest request, HttpServletResponse response,
                                              @ApiParam(name = "keyWord", value = "关键词") @RequestParam(value = "keyWord", required = false) String keyWord,
                                              @ApiParam(name = "classifyId", value = "分类id") @RequestParam(value = "classifyId", required = false) Integer classifyId,
                                              @ApiParam(name = "page", value = "页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                              @ApiParam(name = "limit", value = "条数") @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {

        List<Article> articles = articleService.getArticle(keyWord, classifyId, (page - 1) * limit, limit);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), articles);

    }



    @GetMapping("/{articleId}")
    @ApiOperation("根据id查询详细信息")
    public JsonData<Article> getArticle(HttpServletRequest request, HttpServletResponse response,
                                        @ApiParam(name = "articleId", value = "文章id") @NotNull(message = "articleId 不能为空") @PathVariable(value = "articleId")
                                                Integer articleId
    ) {

        Article articlesInfo = articleService.getArticleInfo(articleId);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), articlesInfo);

    }

}
